/**
 * 
 */
package com.sj.szxy.courseschedule.entity.course;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;

import com.sj.szxy.courseschedule.entity.AdministrativeClass;
import com.sj.szxy.courseschedule.entity.ClassGroup;
import com.sj.szxy.courseschedule.entity.ClassRoom;
import com.sj.szxy.courseschedule.entity.Segment;
import com.sj.szxy.courseschedule.entity.Subject;
import com.sj.szxy.courseschedule.entity.Teacher;

/**
 * 由教学课程组成的一个课程组
 * 
 * @author Yi Ping
 * @date 2018年3月29日 下午9:27:45 
 * @since 1.0.0
 *
 */
public class TeachingCourseBundle implements Course{
	
	private Set<TeachingCourse>  teachingCourses = new HashSet<>();
	private ClassGroup group;
	
	
	
	/**
	 * @param teachingCourses
	 * @param group
	 */
	public TeachingCourseBundle(Collection<TeachingCourse> teachingCourses,  ClassGroup group) {
		Objects.requireNonNull(group, "to bundle teaching courses, class group cannot be null");
		if(teachingCourses.size() ==0) {
			throw new IllegalArgumentException("teachingCourses  can not be null nor zero length");
		}
		
		this.group = group;
		Subject subject = null;
		for (TeachingCourse teachingCourse : teachingCourses) {
			if(group.getTeachingClasses().contains(teachingCourse)) {
				if(subject == null) {
					subject = teachingCourse.getSubject();
				}
				if(teachingCourse.getSubject().equals(subject))
					teachingCourses.add(teachingCourse);
				else {
					throw new RuntimeException("教学班的教学科目不一样， 不能组合在一起");
				}
			}else {
				throw new RuntimeException();
			}
		}
		
	}

	public void addTeachingCourse(TeachingCourse course) {
		
	}
	
	public Set<TeachingCourse> getTeachingCourses() {
		return teachingCourses;
	}

	@Override
	public Set<Teacher> getTeachers() {
		Set<Teacher> teachers = new HashSet<>();
		for (TeachingCourse course : teachingCourses) {
			teachers.add(course.getTeacher());
		}
		return teachers;
	}

	@Override
	public Set<AdministrativeClass> getAdministractiveClasses() {
		return group.getAdministrativeClasses();
	}


	@Override
	public Subject getSubject() {
		Iterator<TeachingCourse> iterator = teachingCourses.iterator();
		if(iterator.hasNext())
			return iterator.next().getSubject();
		return null;
	}

	@Override
	public Set<ClassRoom> getClassRooms() {
		Set<ClassRoom> rooms = new HashSet<>();
		for (TeachingCourse course : teachingCourses) {
			rooms.add(course.getClassRoom());
		}
		
		return rooms;
	}

	@Override
	public void setSegment(Segment segment) {
		for (TeachingCourse teachingCourse : teachingCourses) {
			teachingCourse.setSegment(segment);
		}
	}

	@Override
	public Segment getSegment() {
		Segment min = null;
		for (TeachingCourse teachingCourse : teachingCourses) {
			if(min == null)
				min = teachingCourse.getSegment();
			
			if(min.compareTo(teachingCourse.getSegment()) > 0) {
				min = teachingCourse.getSegment();
			}
		}
		
		return min;
	}

	@Override
	public int compareTo(Course o) {
		return this.getSegment().compareTo(o.getSegment());
	}

	@Override
	public CourseType getType() {
		Iterator<TeachingCourse> iterator = teachingCourses.iterator();
		if(iterator.hasNext())
			return iterator.next().getType();
		return null;
	}
	
	
	
	
}
